<?php

class SchoolAction extends CommonAction{

    function add() {
		$type = M('school_type');
		$typelist = $type->select();

		$this->assign('typelist',$typelist);
    	$this->display();
    }

    function doadd(){
		$xxmc = $this->_isempty($_POST['XXMC'],'学校名称不能为空');
		$sheng = $this->_isempty($_POST['sheng'],'请选择学校所在区域');
		$shi = $this->_isempty($_POST['shi'],'请选择学校所在区域');
		$qu = $this->_isempty($_POST['qu'],'请选择学校所在区域');
		$lxdh = $this->_isempty($_POST['LXDH'],'联系电话不能为空');
		$username = $this->_isempty($_POST['username'],'账号不能为空');
		$password = $this->_isempty($_POST['password'],'密码不能为空');
		$SJHM = $this->_isempty($_POST['SJHM'],'手机号码不能为空');


		$map0['XXMC'] = $xxmc;
		$map0['XXYWMC'] = $_POST['XXYWMC'];
		$map0['XXDM'] = $_POST['XXDM'];
		$map0['XXJJ'] = $_POST['XXJJ'];
		$map0['XXDZ'] = $_POST['XXDZ'];
		$map0['quyu'] = $_POST['sheng'].'-'.$_POST['shi'].'-'.$_POST['qu'];
		$map0['XZXM'] = $_POST['XZXM'];
		$map0['LXDH'] = $lxdh;
		$map0['ZYDZ'] = $_POST['ZYDZ'];
		$map0['CZDH'] = $_POST['CZDH'];
		$map0['DZXX'] = $_POST['DZXX'];
		$map0['school_type_id'] = $_POST['school_type_id'];
		$map0['adminid'] = $_SESSION['adminid'];

		$m = M('school');
		$schoolid = $m->add($map0);
		add_log($_SESSION['adminid'],'添加 ID:'.$schoolid.'学校',2);


		$map1['username'] = $_POST['username'];
		$map1['password'] = $_POST['password'];
		$map1['is_teacher'] = 1;
		$map1['is_school'] = 1;
		$map1['is_parent'] = 0;
		$map1['register_time'] = time();
		$map1['SJHM'] = $_POST['SJHM'];
		$map1['password_des'] = encrypt($_POST['password']);
//		$map1['school_id'] = $schoolid;
//		$map1['admin_id'] = $_SESSION['adminid'];
		$map1['status'] = 1;
		$map1['vcode']=randchar();
		//创建时间
		$map1['addtime']=time();

		$map2['XM'] = $_POST['XZXM'];
		$map2['LXDH'] = $_POST['SJHM'];
		$map2['DZXX'] = $_POST['email'];
		$map2['LXDZ'] = $_POST['XXDZ'];
		$map2['QQ'] = $_POST['QQ'];
//		$map2['user_id'] = $userid;
		$map2['school_id'] = $schoolid;

		$id = TeacherAction::addteacherinfo($schoolid,$map1,$map2,'school');

		if($id >0){
			add_log($_SESSION['adminid'],'添加 ID:'.$id.'教师',2);
			$this->assign('jumpUrl',__URL__.'/lists/');
			$this->assign('waitSecond','1');
			$this->success("添加成功");
		}

    }


	function lists(){
		if($_REQUEST['XXMC'] != null && $_REQUEST['XXMC'] != ""){
			$map['XXMC'] = array('like','%'.$_REQUEST[XXMC].'%');
			$this->assign('XXMC',$_REQUEST['XXMC']);
		}
		if($_REQUEST['sheng'] != null && $_REQUEST['sheng'] != ""){
			$map['quyu']  = array('like','%'.$_REQUEST[sheng].'%');
		}
		if($_REQUEST['shi'] != null && $_REQUEST['shi'] != ""){
			$map['quyu']  = array('like','%'.$_REQUEST[shi].'%');
		}
		if($_REQUEST['qu'] != null && $_REQUEST['qu'] != ""){
			$map['quyu']  = array('like','%'.$_REQUEST[qu].'%');
		}

		if($_REQUEST['sid'] != null && $_REQUEST['sid'] != "" ){
			$map['id']  = $_REQUEST['sid'];
			$this->assign('sid',$_REQUEST['sid']);
		}

//		print_r($map);

		$m = M('school');
		$list = $this->_listpage($m,$map);
		$this->display();
	}


	function edit(){
		$id = $this->_isempty($_GET['id'],'ID不能为空');
		$m = M('school');
		$list = $m->find($id);
		$this->assign('list',$list);

		$type = M('school_type');
		$typelist = $type->find($list['school_type_id']);
		$this->assign('typename',$typelist['type_name']);

		$quyu = $list['quyu'];
		$quyu = explode('-',$quyu);
		$this->assign('quyu0',$quyu[0]);
		$this->assign('quyu1',$quyu[1]);
		$this->assign('quyu2',$quyu[2]);

		$this->display();
	}


	function doedit(){
		$id = $this->_isempty($_POST['id'],'ID不能为空');
		$xxmc = $this->_isempty($_POST['XXMC'],'学校名称不能为空');
		$lxdh = $this->_isempty($_POST['LXDH'],'联系电话不能为空');

		$sheng = $this->_isempty($_POST['sheng'],'请选择学校所在区域');
		$shi = $this->_isempty($_POST['shi'],'请选择学校所在区域');
		$qu = $this->_isempty($_POST['qu'],'请选择学校所在区域');


		$m = M('school');
		$map0['id'] = $id;
		$map0['XXMC'] = $xxmc;
		$map0['XXYWMC'] = $_POST['XXYWMC'];
		$map0['XXDM'] = $_POST['XXDM'];
		$map0['XXJJ'] = $_POST['XXJJ'];
		$map0['XXDZ'] = $_POST['XXDZ'];
		$map0['quyu'] = $_POST['sheng'].'-'.$_POST['shi'].'-'.$_POST['qu'];
		$map0['XZXM'] = $_POST['XZXM'];
		$map0['LXDH'] = $lxdh;
		$map0['ZYDZ'] = $_POST['ZYDZ'];
		$map0['CZDH'] = $_POST['CZDH'];
		$map0['DZXX'] = $_POST['DZXX'];

		$m = M('school');
		$m->save($map0);
		add_log($_SESSION['adminid'],'修改 ID:'.$id.'学校',2);
		$this->success("修改成功",__URL__.'/lists');

	}



	function delete(){
		$id = $this->_isempty($_GET['id'],'ID不能为空');
		$map['school_id'] =$id;
		$mstudent=M('student');
		$mclass =M('class');
		$msubject=M('subject');
		if ($mstudent->where($map)->count()>0 or $mclass->where($map)->count>0 or $msubject->where($map)->count>0)
		{
			$this->error('存在记录的学校不能删除');
		}
		$m = M('school');
		if($m->delete($id)){
			add_log($_SESSION['adminid'],'删除 ID:'.$id.'学校',2);
			$this->assign('jumpUrl',__URL__.'/lists/');
			$this->assign('waitSecond','1');
			$this->success("删除成功");
		}
	}

	function deleteall(){
		$id = $_POST['ids'];
		$m = M('school');
		$data['id'] = array('in',$id);
		$map['school_id'] = array('in',$id);
		$mstudent=M('student');
		$mclass =M('class');
		$msubject=M('subject');
		if ($mstudent->where($map)->count()>0 or $mclass->where($map)->count>0 or $msubject->where($map)->count>0)
		{
			$this->error('存在记录的学校不能删除');
		}
		foreach($id as $k=>$l){
			$m->delete($l);
			add_log($_SESSION['adminid'],'删除 ID:'.$l.'学校',2);
		}
		$this->assign('jumpUrl',__URL__.'/lists/');
		$this->assign('waitSecond','1');
		$this->success("删除成功");
	}

	function loginschool()
	{
		$school_id =$_REQUEST['id'];
		$m=M('school');
		$teacher_info = $m->field('teacher.*,user.username,user.password')->join('teacher on school.id =teacher.school_id')->join('user on user.id =teacher.user_id')->where('user.is_school=1 and school.id='.$school_id)->limit('0,1')->select();
		// print_r($teacher_info);
		// exit;
		 $_SESSION['scode'] = 'web';
		$_SESSION['susername'] = $teacher_info[0]['username'];
		$_SESSION['spassword'] = $teacher_info[0]['password'];
		$_SESSION['suserid'] = $teacher_info[0]['user_id'];
		$_SESSION['school_id'] = $teacher_info[0]['school_id'];
		$_SESSION['teacher_id'] = $teacher_info[0]['id'];
		$_SESSION['type'] = 'school';

		$this->success('操作成功','/index.php/School/index');
		//$this->redirect(__ROOT__.'fadfadf');


	}




}
?>